package cn.stylefeng.guns.modular.hx.storage.controller;

import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.core.util.DateUtil;
import cn.stylefeng.guns.modular.hx.storage.entity.HxStorageCenter;
import cn.stylefeng.guns.modular.hx.storage.entity.HxStorageOut;
import cn.stylefeng.guns.modular.hx.storage.model.params.HxStorageCenterParam;
import cn.stylefeng.guns.modular.hx.storage.model.params.HxStorageOutParam;
import cn.stylefeng.guns.modular.hx.storage.model.result.HxStorageOutResult;
import cn.stylefeng.guns.modular.hx.storage.service.HxStorageCenterService;
import cn.stylefeng.guns.modular.hx.storage.service.HxStorageOutService;
import cn.stylefeng.roses.core.base.controller.BaseController;
import cn.stylefeng.roses.core.reqres.response.ResponseData;
import cn.stylefeng.guns.sys.modular.system.entity.User;
import cn.stylefeng.guns.sys.modular.system.service.UserService;
import org.beetl.ext.fn.Json;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;

import java.util.List;

import static javax.xml.bind.DatatypeConverter.parseInt;


/**
 * 备货区分配控制器
 *
 * @author lww
 * @Date 2023-10-23 11:14:34
 */
@Controller
@RequestMapping("/hxStorageCenter")
public class HxStorageCenterController extends BaseController {

    private String PREFIX = "/hx/storage/hxStorageCenter";

    @Autowired
    private HxStorageCenterService hxStorageCenterService;

    @Autowired
    private HxStorageOutService hxStorageOutService;

    @Autowired
    private UserService userService;

    /**
     * 跳转到	备货口分配
     * 页面
     * @author lww
     * @Date 2023-10-23
     */
    @RequestMapping("to_allocationStockPort")
    public String to_allocationStockPort() {
        return PREFIX + "/allocationStockPort.html";
    }
    
    /**
     * 查询拣货任务，门店
     * @author zhucx
     * @Date 2023-09-05
     */
    @RequestMapping("/findHxStorageOutFenpeiBySpec")
    @ResponseBody
    public LayuiPageInfo findHxStorageOutFenpeiBySpec(HxStorageOutParam hxStorageOutParam) {
    	User user = userService.getcurrentUser();
    	if(hxStorageOutParam.getPeisongTime() == null) {
    		hxStorageOutParam.setPeisongTime(DateUtil.getDate());
    	}
        hxStorageOutParam.setPeisongTime(null);
    	hxStorageOutParam.setDeptId(user.getDeptId());
    	hxStorageOutParam.setOperateId(user.getOperateId());
        hxStorageOutParam.setNotStockPort("notStockPort");
    	LayuiPageInfo layuiPageInfo = this.hxStorageCenterService.findHxStorageOutFenpeiBySpec(hxStorageOutParam);
    	List<HxStorageOutResult> list = layuiPageInfo.getData();
    	for (HxStorageOutResult r : list) {
    		String[] strs = r.getSingleQuantity().split(",");
    		r.setSingleQuantity(strs.length+"");
		}
    	layuiPageInfo.setData(list);
        return layuiPageInfo;
    }

    @RequestMapping("/insertstockPort")
    @ResponseBody
    public ResponseData insertstockPort(String stockports) {
        User user = userService.getcurrentUser();
        //System.out.println(stockports);
        //新增备货口表  备货口
        String[] strs = stockports.split("-");
        //System.out.println(strs);
        int start = parseInt(strs[0]);
        //System.out.println(start);
        int end = parseInt(strs[1]);
        //System.out.println(end);
        if (start<end){
        for (int i=start;i<=end;i++){
            HxStorageCenterParam hxStorageCenterParam = new HxStorageCenterParam();
            String s = String.valueOf(i);
            //System.out.println(i);
            hxStorageCenterParam.setStockPort(s);
                hxStorageCenterParam.setDeptId(user.getDeptId());
                hxStorageCenterParam.setDeliverId(user.getDeliverId());
                hxStorageCenterService.insertStockPort(hxStorageCenterParam);
        }
        }else if (start>=end){

        }
        return ResponseData.success();
    }

    /**
     * 批量分配备货口
     * @author lww
     * @Date 2023-10-23
     */
    @RequestMapping("/fpstockPort")
    @ResponseBody
    public ResponseData fpstockPort(Long getUnitId,String stockPorts,String getUnit,String peisongTime) {
    	User user = userService.getcurrentUser();
    	//更新备货口表  门店名称
		String[] strs = stockPorts.split(",");
		for (String str : strs) {
			HxStorageCenterParam hxStorageCenterParam = new HxStorageCenterParam();
			hxStorageCenterParam.setGetUnit(getUnit);
			hxStorageCenterParam.setStockPort(str);
			hxStorageCenterParam.setDeptId(user.getDeptId());
			hxStorageCenterParam.setOperateId(user.getOperateId());
			hxStorageCenterService.updateBySpec(hxStorageCenterParam);
		}
    	
    	//更新拣货任务表   备货口
    	UpdateWrapper<HxStorageOut> updateWrapper = new UpdateWrapper<HxStorageOut>();
    	updateWrapper.set("stock_port", stockPorts);
    	updateWrapper.eq("peisong_time", peisongTime);
    	updateWrapper.eq("get_unit_id", getUnitId);
    	hxStorageOutService.update(updateWrapper);
        return ResponseData.success();
    }

// 查门店
    @RequestMapping("/chaxun")
    @ResponseBody
    public List<HxStorageOutResult> cha(HxStorageOutParam hxStorageOutParam) {

        List<HxStorageOutResult> getunit = this.hxStorageCenterService.findGetunit(hxStorageOutParam);
        return getunit;
    }

    /**
     * 跳转到主页面
     *
     * @author lww
     * @Date 2023-10-23
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "/hxStorageCenter.html";
    }

    /**
     * 新增页面
     *
     * @author lww
     * @Date 2023-10-23
     */
    @RequestMapping("/add")
    public String add() {
        return PREFIX + "/hxStorageCenter_add.html";
    }

    /**
     * 编辑页面
     *
     * @author lww
     * @Date 2023-10-23
     */
    @RequestMapping("/edit")
    public String edit() {
        return PREFIX + "/hxStorageCenter_edit.html";
    }

    /**
     * 新增接口
     *
     * @author lww
     * @Date 2023-10-23
     */
    @RequestMapping("/addItem")
    @ResponseBody
    public ResponseData addItem(HxStorageCenterParam hxStorageCenterParam) {
        this.hxStorageCenterService.add(hxStorageCenterParam);
        return ResponseData.success();
    }

    /**
     * 编辑接口
     *
     * @author lww
     * @Date 2023-10-23
     */
    @RequestMapping("/editItem")
    @ResponseBody
    public ResponseData editItem(HxStorageCenterParam hxStorageCenterParam) {
        hxStorageCenterParam.setGetUnit("");
        this.hxStorageCenterService.update(hxStorageCenterParam);
        return ResponseData.success();
    }
    @RequestMapping("/clearall")
    @ResponseBody
    public ResponseData clearall(){
        HxStorageCenterParam hxStorageCenterParam=new HxStorageCenterParam();
        User user = userService.getcurrentUser();
        hxStorageCenterParam.setDeptId(user.getDeptId());
        hxStorageCenterParam.setOperateId(user.getOperateId());
        hxStorageCenterParam.setGetUnit(null);
        this.hxStorageCenterService.clearall(hxStorageCenterParam);
        return ResponseData.success();
    }
    /**
     * 删除接口
     *
     * @author lww
     * @Date 2023-10-23
     */
    @RequestMapping("/delete")
    @ResponseBody
    public ResponseData delete(HxStorageCenterParam hxStorageCenterParam) {
        this.hxStorageCenterService.delete(hxStorageCenterParam);
        return ResponseData.success();
    }

    /**
     * 查看详情接口
     *
     * @author lww
     * @Date 2023-10-23
     */
    @RequestMapping("/detail")
    @ResponseBody
    public ResponseData detail(HxStorageCenterParam hxStorageCenterParam) {
        HxStorageCenter detail = this.hxStorageCenterService.getById(hxStorageCenterParam.getId());
        return ResponseData.success(detail);
    }

    /**
     * 查询列表
     *
     * @author lww
     * @Date 2023-10-23
     */
    @ResponseBody
    @RequestMapping("/list")
    public LayuiPageInfo list(HxStorageCenterParam hxStorageCenterParam,String condition ) {
        User user = userService.getcurrentUser();
        hxStorageCenterParam.setDeptId(user.getDeptId());
        hxStorageCenterParam.setDeliverId(user.getDeliverId());
        return this.hxStorageCenterService.findPageBySpec(hxStorageCenterParam,condition);
    }

}


